/*

	This file constructs promotion data at different levels of aggregation

*/

cd ${work}4_reg_variables


*** FIRM-DESTINATION-YEAR LEVEL
use input\promotion, clear

*collapse to idt
collapse (max) treated tdcinitiative (sum) itemvalue discount , by(jurnr_num land year)

*balance in years
reshape wide treated tdcinitiative itemvalue discount , i(jurnr_num land) j(year)
reshape long

*promotion specifications
egen fd=group(jurnr_num land)
xtset fd year
foreach v of varlist treated tdcinitiative {
	replace `v'=0 if `v'==.
	rename `v' `v'_1
	gen `v'_2 = (`v'_1==1 | l.`v'_1==1)	if year>2002
	gen `v'_lag = (l.`v'_1==1)			if year>2002
	gen `v'_lead = (f.`v'_1==1)			if year<2015
}
foreach v of varlist itemvalue discount {
	replace `v'=0 if `v'==.		
	rename `v' `v'_1
	gen `v'_lag = l.`v'_1
}

*save
keep jurnr_num land year treated_* tdcinitiative_* itemvalue_* discount_*
compress
save output\promotion_idt, replace


*** FIRM-PRODUCT-YEAR LEVEL
use jurnr vnr land year using output\export_ipdt, clear

destring jurnr, gen(jurnr_num)
merge m:1 jurnr_num land year using output\promotion_idt, keep(1 3) keepusing(treated_1 tdcinitiative_1) nogen
foreach v of varlist treated_1 tdcinitiative_1 {
	replace `v'=0 if `v'==.
}

*collapse to ipt
collapse (max) treated=treated_1 tdcinitiative=tdcinitiative_1 , by(jurnr_num vnr year)

*balance in years
reshape wide treated tdcinitiative , i(jurnr_num vnr) j(year)
reshape long

*promotion specifications
egen fp=group(jurnr_num vnr)
xtset fp year
foreach v of varlist treated tdcinitiative {
	replace `v'=0 if `v'==.
	rename `v' `v'_1
	gen `v'_2 = (`v'_1==1 | l.`v'_1==1)	if year>2002
}

*save
keep jurnr_num vnr year treated_? tdcinitiative_?
compress
save output\promotion_ipt, replace


*** FIRM-YEAR (only values to create profit measures)
use input\promotion, clear

*collapse to it
collapse (sum) itemvalue discount , by(jurnr_num year)

*balance in years
reshape wide itemvalue discount , i(jurnr_num) j(year)
reshape long

*promotion specifications
xtset jurnr_num year
foreach v of varlist itemvalue discount {
	replace `v'=0 if `v'==.		
	rename `v' `v'_1
	gen `v'_lag = l.`v'_1
}

*save
keep jurnr_num year itemvalue_* discount_*
compress
save output\promotion_it, replace


*** HS4-DESTINATION-YEAR
use jurnr vnr land year using output\export_ipdt, clear

destring jurnr, gen(jurnr_num)
merge m:1 jurnr_num land year using output\promotion_idt, keep(1 3) keepusing(treated_1 tdcinitiative_1) nogen
drop jurnr_num
foreach v of varlist treated_1 tdcinitiative_1 {
	replace `v'=0 if `v'==.
}

replace land="BE" if land=="LU" //TO COMPLY WITH BACI
collapse (max) treated_1 tdcinitiative_1 , by(jurnr land vnr year)

merge m:1 vnr using output\productcodes_p, assert(2 3) keep(3) keepusing(cn4) nogen

*collapse to pd
collapse (sum) treated_sum=treated_1 tdcinitiative_sum=tdcinitiative_1 , by(cn4 land year)

*balance
reshape wide treated_sum tdcinitiative_sum , i(cn4 land) j(year)
reshape long

*promotion specifications
egen pd=group(cn4 land)
xtset pd year
foreach v in treated tdcinitiative {
	
	replace `v'_sum=0 if `v'_sum==.
	rename `v'_sum `v'_sum_1
	gen `v'_sum_2 = `v'_sum_1 + l.`v'_sum if year>2002
		
}

*save
keep cn4 land year treated_* tdcinitiative_*
compress
save output\promotion_pdt, replace